﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using Model;
using WindowsForms.service;
using WindowsForms.Class;

namespace WindowsForms
{
    public partial class TableManagement : DevExpress.XtraEditors.XtraForm
    {
        Service1 iQuery;
        List<Tables> lsTable = null;
        public TableManagement()
        {
            iQuery = new Service1();
            InitializeComponent();
            GetAllTable();
        }

        private void GetAllTable()
        {
            ConvertToList convert = new ConvertToList();
            lsTable = convert.ConvertTolistTables(iQuery.SelectTablesAll().Tables[0]);
            gridControlTable.DataSource = lsTable;
            btnEditTable.Enabled = false;
            btnDelTable.Enabled = false;
        }

        private void btnAddTable_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            using (TableProcess tableProcess = new TableProcess())
            {
                if (tableProcess.ShowDialog(this.FindForm()) == DialogResult.OK)
                {
                    Tables table = new Tables();
                    table.RegionID = tableProcess.Table_RegionID;
                    table.TableName = tableProcess.Table_Name;
                    table.Description = tableProcess.Table_Des;
                    table.IsBlank = false;
                    iQuery.InsertTable(table.TableName,table.RegionID,table.Description,table.IsBlank);
                    lsTable.Add(table);
                }
            }
        }

        private void btnEditTable_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            int[] rows = gvTable.GetSelectedRows();
            Tables tableSelected = gvTable.GetRow(rows[rows.Length - 1]) as Tables;

            using (TableProcess tableProcess = new TableProcess(true))
            {
                //Update Table
                tableProcess.Table_Name = tableSelected.TableName;
                tableProcess.Table_Des = tableSelected.Description;
                tableProcess.Table_RegionID = tableSelected.RegionID;

                if (tableProcess.ShowDialog(this.FindForm()) == DialogResult.OK)
                {
                    tableSelected.TableName = tableProcess.Table_Name;
                    tableSelected.Description = tableProcess.Table_Des;
                    tableSelected.RegionID = tableProcess.Region_Edit.RegionID;

                    iQuery.UpdateTable(tableSelected.TableID,tableSelected.TableName,tableSelected.RegionID,tableSelected.Description
                        ,true,false);
                    gvTable.RefreshData();
                }
            }
        }

        private void btnDelTable_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            int[] rows = gvTable.GetSelectedRows();
            Tables tableSelected = gvTable.GetRow(rows[rows.Length - 1]) as Tables;
            string message = string.Format(Constants.MESS_CONFIRMDELETE, tableSelected.TableName);
            DialogResult dr = MessageBox.Show(message, Constants.MESS_DELETE_CAPTION, MessageBoxButtons.YesNo);
            if (dr == DialogResult.Yes)
            {
                
                iQuery.DeleteTable(tableSelected.TableID);
                lsTable.Remove(tableSelected);
                gvTable.RefreshData();
            }
        }

        private void gvTable_SelectionChanged(object sender, DevExpress.Data.SelectionChangedEventArgs e)
        {
            btnDelTable.Enabled = gvTable.SelectedRowsCount > 0;
            btnEditTable.Enabled = gvTable.SelectedRowsCount > 0;
        }

        private void btnRefresh_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            GetAllTable();
        }

        private void TableManagement_Load(object sender, EventArgs e)
        {

        }
    }
}